﻿@model QueuedEmailListModel
@inject AdminAreaSettings adminAreaSettings
@{
    //page title
    ViewBag.Title = T("Admin.System.QueuedEmails").Text;
}
<form asp-area="@Constants.AreaAdmin" asp-controller="QueuedEmail" asp-action="List" method="post">
    
    <div class="row">
        <div class="col-md-12">
            <div class="x_panel light form-fit">
                <div class="x_title">
                    <div class="caption">
                        <i class="fa fa-envelope"></i>
                        @T("Admin.System.QueuedEmails")
                    </div>
                    <div class="actions btn-group btn-group-devided">
                        <input type="button" id="delete-selected" class="btn green" value="@T("Admin.System.QueuedEmails.DeleteSelected")" />
                        <input type="submit" name="delete-all" class="btn red" value="@T("Admin.System.QueuedEmails.DeleteAll")" onclick="return confirm('@T("Admin.Common.AreYouSure")'); " />
                        <vc:admin-widget widget-zone="queued_email_list_buttons" additional-data="null" />
                    </div>
                </div>
                <div class="x_content form">
                    <div class="form-horizontal">
                        <div class="form-body">
                            <div class="form-group">
                                <admin-label asp-for="SearchStartDate" />
                                <div class="col-md-4 col-sm-6">
                                    <admin-input asp-for="SearchStartDate" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="SearchEndDate" />
                                <div class="col-md-4 col-sm-6">
                                    <admin-input asp-for="SearchEndDate" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="SearchFromEmail" />
                                <div class="col-md-4 col-sm-6">
                                    <admin-input asp-for="SearchFromEmail" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="SearchToEmail" />
                                <div class="col-md-4 col-sm-6">
                                    <admin-input asp-for="SearchToEmail" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="SearchText" />
                                <div class="col-md-9 col-sm-9">
                                    <admin-input asp-for="SearchText" />
                                    <span asp-validation-for="SearchText"></span>
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="SearchLoadNotSent" />
                                <div class="col-md-4 col-sm-6">
                                    <label class="mt-checkbox mt-checkbox-outline control control-checkbox">
                                        <admin-input asp-for="SearchLoadNotSent" />
                                        <div class="control__indicator"></div>
                                    </label>
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="SearchMaxSentTries" />
                                <div class="col-md-4 col-sm-6">
                                    <admin-input asp-for="SearchMaxSentTries" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="GoDirectlyToNumber" />
                                <div class="col-md-4 col-sm-6">
                                    <admin-input asp-for="GoDirectlyToNumber" />
                                </div>
                                <div class="col-md-1 col-sm-1">
                                    <input type="submit" name="go-to-email-by-number" class="btn default" value="@T("Admin.Common.Go")" />
                                </div>
                            </div>
                        </div>
                        <div class="form-actions">
                            <div class="row">
                                <div class="offset-md-3 offset-sm-3 col-md-9 col-sm-9">
                                    <button class="btn btn-success filter-submit" id="search-queuedemails">
                                        <i class="fa fa-search"></i> @T("Admin.Common.Search")
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="x_content">
                    <div id="queuedEmails-grid"></div>
                </div>
            </div>
        </div>
    </div>

    <script>
        $(document).ready(function () {
            $("#queuedEmails-grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("QueuedEmailList", "QueuedEmail"))",
                            type: "POST",
                            dataType: "json",
                            data: additionalData
                        }
                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors"
                    },
                    error: function(e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    },
                    pageSize: @(adminAreaSettings.DefaultGridPageSize),
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true
                },
                pageable: {
                    refresh: true,
                    pageSizes: [@(adminAreaSettings.GridPageSizes)]
                },
                scrollable: false,
                dataBound: onDataBound,
                columns: [{
                    field: "Id",
                    headerTemplate: "<label class='mt-checkbox mt-checkbox-outline control control-checkbox'><input id='mastercheckbox' type='checkbox'/> <div class='control__indicator'></div></label>",
                    headerAttributes: { style: "text-align:center" },
                    attributes: { style: "text-align:center" },
                    template: "<label class='mt-checkbox mt-checkbox-outline control control-checkbox'><input type='checkbox' value='#=Id#' class='checkboxGroups'/> <div class='control__indicator'></div></label>",
                    width: 60
                }, {
                    field: "Subject",
                    title: "@T("Admin.System.QueuedEmails.Fields.Subject")",
                    width: 300,
                    template: '<a class="k-link" href="Edit/#=Id#">#=kendo.htmlEncode(Subject)#</a>',
                }, {
                    field: "From",
                    title: "@T("Admin.System.QueuedEmails.Fields.From")",
                    width: 100
                }, {
                    field: "To",
                    title: "@T("Admin.System.QueuedEmails.Fields.To")",
                    width: 100
                }, {
                    field: "CreatedOn",
                    title: "@T("Admin.System.QueuedEmails.Fields.CreatedOn")",
                    width: 100,
                    type: "date",
                    format: "{0:G}"
                }, {
                    field: "DontSendBeforeDate",
                    title: "@T("Admin.System.QueuedEmails.Fields.DontSendBeforeDate")",
                    width: 100,
                    type: "date",
                    format: "{0:G}"
                }, {
                    field: "SentOn",
                    title: "@T("Admin.System.QueuedEmails.Fields.SentOn")",
                    width: 100,
                    type: "date",
                    format: "{0:G}"
                }, {
                    field: "PriorityName",
                    title: "@T("Admin.System.QueuedEmails.Fields.Priority")",
                    width: 50
                }]
            });
        });
    </script>
    <script>
        var selectedIds = [];

        $(document).ready(function () {

            //search button
            $('#search-queuedemails').click(function () {
                //search
                var grid = $('#queuedEmails-grid').data('kendoGrid');
                grid.dataSource.page(1); //new search. Set page size to 1
                //grid.dataSource.read(); we already loaded the grid above using "page" function
                //clear selected checkboxes
                $('.checkboxGroups').prop('checked', false).change();
                selectedIds = [];
                return false;
            });

            $("#@Html.FieldIdFor(model => model.SearchFromEmail)").keydown(function (event) {
                if (event.keyCode == 13) {
                    $("#search-queuedemails").click();
                    return false;
                }
            });

            $("#@Html.FieldIdFor(model => model.SearchToEmail)").keydown(function (event) {
                if (event.keyCode == 13) {
                    $("#search-queuedemails").click();
                    return false;
                }
            });

            $("#@Html.FieldIdFor(model => model.SearchText)").keydown(function (event) {
                if (event.keyCode == 13) {
                    $("#search-queuedemails").click();
                    return false;
                }
            });
            //"delete selected" button
            $('#delete-selected').click(function (e) {
                e.preventDefault();

                var postData = {
                    selectedIds: selectedIds
                };
                addAntiForgeryToken(postData);

                $.ajax({
                    cache:false,
                    type: "POST",
                    url: "@(Url.Action("DeleteSelected", "QueuedEmail"))",
                    data: postData,
                    complete: function (data) {
                        //reload grid
                        var grid = $('#queuedEmails-grid').data('kendoGrid');
                        grid.dataSource.page(1); //new search. Set page size to 1
                        //grid.dataSource.read(); we already loaded the grid above using "page" function
                        //clear selected checkboxes
                        $('.checkboxGroups').prop('checked', false).change();
                        selectedIds = [];
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert(thrownError);
                    },
                    traditional: true
                });
                return false;
            });

            $('#mastercheckbox').click(function () {
                $('.checkboxGroups').prop('checked', $(this).is(':checked')).change();
            });

            //wire up checkboxes.
            $('#queuedEmails-grid').on('change', 'input[type=checkbox][id!=mastercheckbox]', function (e) {
                var $check = $(this);
                if ($check.is(":checked") == true) {
                    var checked = jQuery.inArray($check.val(), selectedIds);
                    if (checked == -1) {
                        //add id to selectedIds.
                        selectedIds.push($check.val());
                    }
                }
                else {
                    var checked = jQuery.inArray($check.val(), selectedIds);
                    if (checked > -1) {
                        //remove id from selectedIds.
                        selectedIds = $.grep(selectedIds, function (item, index) {
                            return item != $check.val();
                        });
                    }
                }
                updateMasterCheckbox();
            });
        });

        function additionalData() {
            var data = {
                SearchStartDate: $('#@Html.FieldIdFor(model => model.SearchStartDate)').val(),
                SearchEndDate: $('#@Html.FieldIdFor(model => model.SearchEndDate)').val(),
                SearchFromEmail: $('#@Html.FieldIdFor(model => model.SearchFromEmail)').val(),
                SearchToEmail: $('#@Html.FieldIdFor(model => model.SearchToEmail)').val(),
                SearchText: $('#@Html.FieldIdFor(model => model.SearchText)').val(),
                SearchLoadNotSent: $('#@Html.FieldIdFor(model => model.SearchLoadNotSent)').is(':checked'),
                SearchMaxSentTries: $('#@Html.FieldIdFor(model => model.SearchMaxSentTries)').val()
            };
            addAntiForgeryToken(data);
            return data;
        }

        function onDataBound(e) {

            $('#queuedEmails-grid input[type=checkbox][id!=mastercheckbox]').each(function () {
                var currentId = $(this).val();
                var checked = jQuery.inArray(currentId, selectedIds);
                //set checked based on if current checkbox's value is in selectedIds.
                $(this).prop('checked', checked > -1);
            });

            updateMasterCheckbox();
        }

        function updateMasterCheckbox() {
            var numChkBoxes = $('#queuedEmails-grid input[type=checkbox][id!=mastercheckbox]').length;
            var numChkBoxesChecked = $('#queuedEmails-grid input[type=checkbox][id!=mastercheckbox]:checked').length;
            $('#mastercheckbox').prop('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
        }
    </script>
</form>